package chapter14.section2;

//程序14-6b：表格模型TransactionTableModel

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

public class TransactionTableModel extends AbstractTableModel {
    private List<Transaction> trans = new ArrayList<Transaction>();

    public TransactionTableModel() {
        // 注意在Transaction类中添加对应的构造方法
        trans.add(new Transaction("001", "2022-04-10", "deposit", 100, 10100, true));
        trans.add(new Transaction("003", "2022-04-10", "deposit", 100, 10200, true));
        trans.add(new Transaction("002", "2022-04-10", "withdraw", 100, 10100, true));

    }

    // 返回表格显示所需的列名
    public String getColumnName(int column) {
        return Transaction.columnModel[column];
    }

    // 向集合中增加一行数据，需要通知监听器哪一行发生了变化
    public void add(Transaction tran) {
        this.trans.add(tran);
//通知表格重新显示
        this.fireTableRowsInserted(trans.size() - 1, trans.size() - 1);
    }

    // 表格的总列数
    public int getColumnCount() {
        return 6;
    }

    // 表格的总行数
    public int getRowCount() {
        return trans.size();
    }

    // 表格的某一单元格的内容，注意这里用到了Transaction中的get(index)方法
    public Object getValueAt(int rowIndex, int columnIndex) {
        Transaction t = trans.get(rowIndex);
        return t.get(columnIndex);
    }
}